#ifndef _ODN_SCENE_COLLISIONHANDLER_H
#define _ODN_SCENE_COLLISIONHANDLER_H

#include "odn_phys_Point.h"
#include "odn_phys_Collider.h"

using ouden::physics::ODN_Collider;
using ouden::physics::ODN_CircleCollider;
using ouden::physics::ODN_LineCollider;
using ouden::physics::ODN_DynamicPoint;

namespace ouden
{
	namespace scene
	{
		
		//The collision manager system
		class ODN_CollisionManager//: public ODN_SceneElement
		{
		
		public:
			
			//Allocates all colliders and dynamic points into sectors for quick lookup 
			bool Sectorise();
			
			//Update all the dynamic points 
			bool updateDynamicPoints()
			{
				for (int i=0;i<dynamicpoints.size();i++) {
					dynamicpoints[i]->Update();
				}
			};
			
			//Main function to check for collisions between all the colliders 
			bool interactColliders();
			
			//TODO: Do this with size from scene 
			void setSectorSize(Vector3D SectorSize)		
			{
				//Store the sector size 
				sectorsize=SectorSize;
				
				//Calculate the number of sectors 
				sectorcount = this->size / sectorsize;
				
				//Resize the array of horizontal sectors 
				sectors.resize(int(sectorcount[0]));
				
				//Loop through the horizontal sectors 
				for (int i=0;i<sectors.size();i++) {
					
					//Resize the vertical sectors 
					sectors[i].resize(int(sectorcount[1]));
					
				}
				
			}
		 
		//TODO: See what this should be 
		private:
			
			//Collision detection routines 
			bool checkCollide(ODN_CircleCollider* c1, ODN_CircleCollider* c2);
			bool checkCollide(ODN_LineCollider* l1, ODN_LineCollider* l2);
			bool checkCollide(ODN_CircleCollider* c, ODN_LineCollider* l);
			bool checkCollide(ODN_LineCollider* l, ODN_CircleCollider* c)		{ return checkCollide(c, l); } 
			
			//Array of all the colliders 
			std::vector<ODN_Collider*> colliders;
			
			//Array of all points from all physics models which can be moved and interacted with 
			std::vector<ODN_DynamicPoint*> dynamicpoints;
			
			//Sector system components
			//------------------------
			
			//Size and number of sectors 
			Vector3D sectorsize;
			Vector3D sectorcount;
			
			//Sector structure containing arrays of objects 
			struct sSector { 
				std::vector<ODN_Collider*> colliders; 
				std::vector<ODN_DynamicPoint*> dynamicpoints;
			};
			
			//2 dimensional array of sectors 
			std::vector<std::vector<sSector > > sectors;
			
			//TODO: getNearby functions 
			
			//Functions that return an array of all the colliders in nearby sectors from a collider 
			std::vector<ODN_Collider*> getNearbyColliders(ODN_PointCollider* Collider);
			std::vector<ODN_Collider*> getNearbyColliders(ODN_LineCollider* Collider)		{}
			
			//Function to return an array of all the dynamic points in nearby sectors from a point 
			std::vector<ODN_DynamicPoint*> getNearbyDynamicPoints(ODN_Point* Point/*, float Radius*/)		{}
			
			//------------------------
		
		};
		
	} //namespace scene
} //namespace ouden 

#endif //_ODN_SCENE_COLLISIONHANDLER_H 
